package edu.lingnan.lingnanblog.controller;

import edu.lingnan.lingnanblog.entity.BlogType;
import edu.lingnan.lingnanblog.entity.PageBean;
import edu.lingnan.lingnanblog.service.BlogClientService;
import edu.lingnan.lingnanblog.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuyuan
 * @since 2020-06-17 13:18
 */

@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeConsumerController {

    @Resource
    private BlogClientService blogTypeService;

    @RequestMapping("/list")
    public String list(@RequestParam(value="page",required=false)String page, @RequestParam(value="rows",required=false)String rows, HttpServletResponse response)throws Exception{
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<BlogType> blogTypeList=blogTypeService.listType(map);
        Long total=blogTypeService.getTypeTotal(map);
        JSONObject result=new JSONObject();
        JSONArray jsonArray=JSONArray.fromObject(blogTypeList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    @RequestMapping("/save")
    public String save(BlogType blogType, HttpServletResponse response)throws Exception{
        int resultTotal=0;
        if(blogType.getId()==null){
            resultTotal=blogTypeService.addType(blogType);
        }else{
            resultTotal=blogTypeService.updateType(blogType);
        }
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
        }else{
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value="ids")String ids, HttpServletResponse response)throws Exception{
        System.out.println(ids);
        String []idsStr=ids.split(",");
        JSONObject result=new JSONObject();
        for(int i=0;i<idsStr.length;i++){
            if(blogTypeService.getBlogByTypeId(Integer.parseInt(idsStr[i]))>0){
                result.put("exist", "该类别下您已经发布了博客，不能删除！");
            }else{
                blogTypeService.deleteType(Integer.parseInt(idsStr[i]));
            }
        }
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }
    @RequestMapping("toEditBlogType")
    public String toEditBlogType(){
        return "admin/blogTypeManage";
    }
}
